from analysis.analysis_preprocessing import load_file
from utils import read_config,get_directory
import os
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import silhouette_score
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from sklearn.datasets import make_blobs
from sklearn.decomposition import PCA
import numpy as np


# 读取配置信息
config = read_config()
file_name = "consecutive_rising_day.csv"
_base_directory = config.get('settings','base_file')
_analysis_directory = config.get('settings','analysis_file')
base_path = get_directory(_base_directory,_analysis_directory)

def save_consecutive_rising_day(file_name = file_name):
    df = load_file();
    df = df.query('consecutive_rising_day == 1 and low != high')
    __file = os.path.join(base_path, file_name)
    df.to_csv(__file, index=False, encoding='utf-8-sig')

if __name__=='__main__':
    _file_name = 'consecutive_rising_day_clean.csv'
    # save_consecutive_rising_day(_file_name)
    df = load_file(_file_name)
    targets = [
        ['十字星','prev_doji == True' ],
        ['长上影线','prev_shooting_star  == True' ],
        ['长下影线','prev_dragonfly_doji == True' ],
        ['锤子线', 'prev_hammer == True'],
        ['倒锤子', 'prev_inverted_hammer == True'],
        ['星线', 'prev_star == True'],
        ['小阴线', 'prev_little_black_candle == True'],
        ['中阴线', 'prev_medium_black_candle == True'],
        ['大阴线', 'prev_long_black_candle == True'],
        ['小阳线', 'prev_little_white_candle == True'],
        ['中阳线', 'prev_medium_white_candle == True'],
        ['大阳线', 'prev_long_white_candle == True'],
        ['风高浪急线', 'prev_windy_wavey_candle == True'],
        ['当前 < 昨日成交量', 'volume < pre_volume'],
        ['当前 < 昨日成交量1.2', 'pre_volume <= volume < pre_volume * 1.2'],
        ['当前 < 昨日成交量1.5', 'pre_volume * 1.2 <= volume < pre_volume * 1.5'],
        ['当前 > 昨日成交量1.5', 'pre_volume * 1.5<= volume'],
        ['上升5%', '0 <= prev_trend <5'],
        ['上升5-10%', '5<= prev_trend <10'],
        ['上升>10%', 'prev_trend > 10'],
        ['下降5%', '-5 <= prev_trend < 0'],
        ['下降5-10%', '-10<= prev_trend < 5'],
        ['下降>10%', 'prev_trend < -10'],
        ['下降5-10% 中阴线', '-10<= prev_trend < 5 and prev_medium_black_candle == True'],
        ['下降5% 中阴线', '-5 <= prev_trend < 0  and prev_medium_black_candle == True'],
        ['下降5-10% 星线', '-10<= prev_trend < 5 and prev_star == True'],
        ['下降5% 星线', '-5 <= prev_trend < 0  and prev_star == True'],
        ['下降5-10% 小阴线', '-10<= prev_trend < 5 and prev_little_black_candle == True'],
        ['下降5% 小阴线', '-5 <= prev_trend < 0  and prev_little_black_candle == True'],
        ['下降5-10% 小阳线', '-10<= prev_trend < 5 and prev_little_white_candle == True'],
        ['下降5% 小阳线', '-5 <= prev_trend < 0  and prev_little_white_candle == True'],
        ['后两天不低于open','open <low_next_2_days < close '],
        ['后五天不低于open','open<low_next_5_days<close'],
        ['后两天不低于open and 成交量1.5', 'open <low_next_2_days < close and pre_volume * 1.5<= volume'],
        ['后五天不低于open and 成交量1.5', 'open<low_next_5_days<close and pre_volume * 1.5<= volume']
    ]
    rs = []
    # 使用嵌套循环遍历二维数组
    for row in targets:
        ndf = df.query(row[1])
        ndf_2ll = ndf.query('low_next_2_days < (close+open)/2')
        ndf_5CH = ndf.query('high_close_next_5_days > close * 1.1')
        ndf_5ll = ndf.query('low_next_5_days < (close+open)/2')
        ndf_10CH = ndf.query('high_close_next_10_days > close * 1.2')
        ndf_10ll = ndf.query('low_low_next_10_days < (close+open)/2')
        ndf_2a = ndf.query('low_next_2_days < (close+open)/2 and high_close_next_5_days > close * 1.1')
        _total = len(ndf)
        rs.append([row[0],_total,(len(ndf_2ll)/_total)*100,(len(ndf_5CH)/_total)*100
                      ,(len(ndf_5ll)/_total)*100,(len(ndf_10CH)/_total)*100,(len(ndf_10ll)/_total)*100,(len(ndf_2a)/len(ndf_2ll)*100)])
        # ndf.to_csv('out_low_10.csv', index=False, header=True)
    column_names = ['类型', '总量', '2日最低价/0.5','5日最高收盘价','5日最低价/0.5','10日最高收盘价','10日最低价/0.5','2日低价5日概率']
    rs_df = pd.DataFrame(rs,columns = column_names)
    rs_df.to_csv('out_put.csv', index=False, header=True)



